﻿using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Enums;
using SelfMadeMillionaire.Core.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SelfMadeMillionaire.DAL.Repositories
{
	public class TrackingPortfolioRepository : Repository<TrackingPortfolioInfo>, ITrackingPortfolioRepository
	{
		public TrackingPortfolioRepository(IUnitOfWork uow):base(uow)
		{

		}

		public TrackingPortfolioInfo Get(TrackingPortfolioType tpType)
		{
            return _dbSet.Where(x => x.TPType == (byte)tpType).SingleOrDefault();
		}

		public void Update(TrackingPortfolioInfo entity)
		{
			var info = Get((TrackingPortfolioType)entity.TPType);

			if (info == null)
				_dbSet.Add(entity);
			else
			{
				info.FromDate = entity.FromDate;
				info.ToDate = entity.ToDate;

				info.TrackingPortfolio = entity.TrackingPortfolio;
				info.DowIndustrials = entity.DowIndustrials;
				info.SP500 = entity.SP500;
				info.Nasdaq = entity.Nasdaq;

                info.TrackingPortfolioReturn = entity.TrackingPortfolioReturn;
                info.DowIndustrialsReturn = entity.DowIndustrialsReturn;
                info.SP500Return = entity.SP500Return;
                info.NasdaqReturn = entity.NasdaqReturn;
			}

			_uow.Commit();
		}
	}
}
